<?php

namespace app\modules\lenders\controllers;

use Yii;
use app\modules\lenders\models\Lendinvestment;
use app\modules\lenders\models\search\LendinvestmentSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\modules\lenders\models\TimeSchedule;

/**
 * LendinvestmentController implements the CRUD actions for Lendinvestment model.
 */
class LendinvestmentController extends Controller {

    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Lendinvestment models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new LendinvestmentSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Lendinvestment model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Lendinvestment model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Lendinvestment();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->lendInvestID]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * 报表月月金返息表
     */
    public function actionInterest() {
        $searchModel = new LendinvestmentSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('interest', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * 报表管理-月月金返息
     * 点击发送短信月月金
     */
    public function actionSendInters() {
        $session = \Yii::$app->session;
        //读取一个Session
        $sql = $session->get('sql_yueyuejin');
            if (!empty($sql)) {
                //替换sql AND未&&
                $sqlstr = str_replace("AND", "&&", $sql);
                $_schedule = new TimeSchedule();
                $_schedule->processedArgs = $sqlstr;
                $_schedule->scheduleName = 'SendYueYueJinSms';
                $_schedule->isProcessed = 0;
                $_schedule->CreateTime = date('Y-m-d H:i:s');
                if ($_schedule->save()) {
                    //发送短信

                    echo json_encode(['status' => 'success', 'message' => '成功!']);
                } else {
                    echo json_encode(['status' => 'error', 'message' => '失败!']);
                }
            } else {
                echo json_encode(['status' => 'error', 'message' => '请您先查询数据1!']);
            }
    }

    /**
     * Updates an existing Lendinvestment model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->lendInvestID]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Lendinvestment model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Lendinvestment model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Lendinvestment the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Lendinvestment::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
